﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.DataModel;

namespace Room
{
    public class RCAIRevivePoint : RCAIUnionFightBase
    {
        #region 数据
        public InstUFRevivePoint instData;
        public TableUnionFightRevivePoint dictData;
        #endregion

        GameObject aloFlag;
        GameObject saoFlag;


        public int saoCount;
        public int aloCount;

        GameObject revive_ing;
        GameObject revive_ed;

        public override void Tick()
        {
            base.Tick();

            var room = RoomManager.currentRoom as RoomUnionFight;
            if (room != null)
            {
                List<Vector3> pos = new List<Vector3>();
                var charactors = room.charactorManager.GetCharactors();
                foreach(var charactor in charactors)
                {
                    if(charactor.Value.type == CharactorType.UnionFightSelf 
                        || charactor.Value.type == CharactorType.UnionFightOther)
                    {
                        pos.Add(charactor.Value.transform.position);
                    }
                }


                var mpPos = owner.transform.position;
                var vec0 = new Vector2(mpPos.x, mpPos.z);

                int Occupy = 0;
                foreach (var _pos in pos)
                {
                    var vec1 = new Vector2(_pos.x, _pos.z);
                    if(Vector2.Distance(vec0,vec1)<= dictData.OccupyRange)
                    {
                        Occupy++;
                    }
                }

                if(Occupy<=0)
                {
                    if (revive_ing != null)
                        revive_ing.SetActive(false);
                    if (revive_ed != null)
                        revive_ing.SetActive(false);

                    var ui = owner.ui as RCUIUnionRevivePoint;
                    ui.SetBarShow(false);
                }
            }
        }

        public void SetCount(int saoCount,int aloCount)
        {
            this.saoCount = saoCount;
            this.aloCount = aloCount;
            
            if (revive_ing==null)
            {
                revive_ing = Spawn(RoomUnionFight.REVIVE_OCCUPY_ING);
            }
            else
            {
                revive_ing.SetActive(true);
            }

            if(revive_ed!=null && revive_ed.activeSelf)
            {
                revive_ed.SetActive(false);
            }
            
        }

        public void InitCamp(int camp)
        {
            if(camp==0)
            {
                saoFlag = Spawn(RoomUnionFight.REVIVE_SAO);
            }

            if(camp == 1)
            {
                aloFlag = Spawn(RoomUnionFight.REVIVE_ALO);
            }
            
            this.camp = camp;
            if (saoFlag != null)
                saoFlag.SetActive(false);
            if (aloFlag != null)
                aloFlag.SetActive(false);

            if (camp == 0 && saoFlag != null)
                saoFlag.SetActive(true);
            
            if (camp == 1 && aloFlag != null)
                aloFlag.SetActive(true);
            
        }
        public override void SetCamp(int camp)
        {
            base.SetCamp(camp);

            if (saoFlag != null)
                saoFlag.SetActive(false);
            if (aloFlag != null)
                aloFlag.SetActive(false);

            if (camp == 0 && saoFlag != null)
                saoFlag.SetActive(true);
            if (camp == 1 && aloFlag != null)
                aloFlag.SetActive(true);

            if (camp == 0 || camp == 1)
            {
                if (revive_ing != null && revive_ing.activeSelf)
                {
                    revive_ing.SetActive(false);
                }

                if (revive_ed == null)
                {
                    revive_ed = Spawn(RoomUnionFight.REVIVE_OCCUPY_ED);
                }
                else
                {
                    revive_ed.SetActive(true);
                }
                var ui = owner.ui as RCUIUnionRevivePoint;
                ui.ShowOccupyEffect(camp);
            }
        }

        public RCAIRevivePoint(RoomCharactor owner)
            : base(owner)
        {
            this.type = AIType.UnionRevivePoint;
        }

        public void Init(InstUFRevivePoint instData)
        {
            UpdateData(instData);
        }

        public void UpdateData(InstUFRevivePoint instData)
        {
            this.instData = instData;
            this.dictData = Dict.Blo.DictUnionFightRevivePointBlo.GetUnionFightRevivePointByID(instData.id + "");

            var url = dictData.ModelUrl;
            FindSceneObj(url);
            //SetEffectNode(url);
        }


        public override void DoStop()
        {
            base.DoStop();

            if (aloFlag != null)
            {
                Unspawn(aloFlag);
            }

            if (saoFlag != null)
            {
                Unspawn(saoFlag);
            }

            if (revive_ing != null)
            {
                Unspawn(revive_ing);
            }

            if(revive_ed!=null)
            {
                Unspawn(revive_ed);
            }

        }

        //void SetEffectNode(string url)
        //{
        //    if (aloFlag == null || saoFlag == null)
        //    {
        //        if (!string.IsNullOrEmpty(dictData.EffectNode))
        //        {
        //            try
        //            {
        //                var sss = dictData.EffectNode.Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
        //                var alo = sss[0];
        //                var sao = sss[1];

        //                aloFlag = sceneObj.transform.parent.Find(alo).gameObject;
        //                saoFlag = sceneObj.transform.parent.Find(sao).gameObject;
        //            }
        //            catch (System.Exception e)
        //            {
        //                Debug.LogError("[ERROR] revive point url:[" + url + "]  effect node:[" + dictData.EffectNode + "]");
        //            }
        //        }
        //    }
        //}
    }
}